package com.briup.authentication.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 设置密码加密工具类
    @Bean
    public PasswordEncoder  passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 在这里固定了，代表lisi用户给第三方应用授权
        // lisi就是资源拥有者
        auth.inMemoryAuthentication()
            .withUser("lisi")
            .password(passwordEncoder().encode("123"))
            .authorities("/*");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 基础认证
        http.httpBasic()
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and().csrf().disable();
    }
}
